﻿using MediatR;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.BuildingD
{
    public class ColumnsSeachListHandler : IRequestHandler<ColumnsSeachListCommon, Result>
    {
        #region 仓储
        /// <summary>
        /// 仓储
        /// </summary>
        public readonly IBaseRepository<Building> BuildingRepository;
        public readonly IBaseRepository<BuildingType> BuildingTypeRepository;
        public readonly IBaseRepository<Columns> ColumnsRepository;
        public readonly ILogger<ColumnsSeachListHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="buildingRepository">栋舍信息</param>
        /// <param name="buildingTypeRepository">栋舍类型</param>
        /// <param name="logger">日志</param>
        public ColumnsSeachListHandler(IBaseRepository<Building> buildingRepository, IBaseRepository<BuildingType> buildingTypeRepository, IBaseRepository<Columns> ColumnsRepository, ILogger<ColumnsSeachListHandler> logger)
        {
            BuildingRepository = buildingRepository;
            BuildingTypeRepository = buildingTypeRepository;
            this.logger = logger;
            this.ColumnsRepository = ColumnsRepository;
        }
        #endregion

        #region 栋舍信息显示列表
        /// <summary>
        /// 栋舍信息显示列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(ColumnsSeachListCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            //异常处理
            try
            {
                logger.LogInformation("栋舍信息显示列表成功!");
                var data = new pagesssDto() { };
                var list = (from a in ColumnsRepository.GetAll().Where(x=>x.BuildingId==request.buildingId)
                            select new ColumnsDto()
                            {
                                ColumnsId = a.ColumnsId,
                                ColumnsName = a.ColumnsName,
                            }).ToList();

                //分页
                data.totalCounts = list.Count();
                data.pageCounts = (int)Math.Ceiling((list.Count() * 1.0) / request.pageSizes);
                data.data = list.OrderBy(x => x.ColumnsId).Skip((request.pageIndexs - 1) * request.pageSizes).Take(request.pageSizes).ToList();

                result.Code = (int)CommonEnumCode.success;
                result.Data = data;
                result.Msg = "栋舍信息显示列表成功";

            }
            catch (Exception ex)
            {
                logger.LogError("栋舍信息显示列表失败!" + ex.Message);
                result.Msg = "栋舍信息显示列表失败";
            }
            return result;
        }
        #endregion
    }

    /// <summary>
    /// 分页
    /// </summary>
    public class pagesssDto()
    {
        public int totalCounts { get; set; }
        public int pageCounts { get; set; }
        public List<ColumnsDto> data { get; set; }
    }

    /// <summary>
    /// 栏位信息Dto
    /// </summary>
    public class ColumnsDto()
    {
        /// <summary>
        /// 栏位Id
        /// </summary>
        public int ColumnsId { get; set; }
        /// <summary>
        /// 栏位名称
        /// </summary>
        public string? ColumnsName { get; set; }
    }
}
